package ui.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

import bean.Adress;
import bean.LineItem;
import bean.Order;

/**
 * Panel for the on-screen invoice view
 * @author Martin Schophaus
 */
@SuppressWarnings("serial")
public class InvoicePanel extends JPanel{
	private Order order;
	
	/**
	 * @param frame main frame
	 * @param order Order for which the invoice is displayed
	 */
	public InvoicePanel (JFrame frame, Order order) {

		this.order = order;
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints constr = new GridBagConstraints();
		
		constr.insets = new Insets(10, 10, 10, 10);
		
		constr.gridx = 0;
		constr.gridy = 0;
		JLabel header = new JLabel(this.order.getCustomer_id() + " : " + this.order.getCustomer().getName() + ", " + this.order.getCustomer().getFname());
		this.add(header, constr);
		
		constr.gridx = 0;
		constr.gridy = 1;
		JLabel header_adress = new JLabel("Adress");
		this.add(header_adress, constr);
		
		constr.gridx = 0;
		constr.gridy = 2;
		Adress adress = this.order.getCustomer().getAdress();
		JTextArea adress_area = new JTextArea(String.format(
				"%s\n%s %s\nTel: %s\nMobil: %s\nFax: %s\nE-Mail: %s",
				adress.getStreet(),
				adress.getPostal_cd(),
				adress.getCity(),
				adress.getTel(),
				adress.getTel_mobile(),
				adress.getTel_fax(),
				adress.getEmail()
				));
		this.add(adress_area, constr);
		
		constr.gridx = 1;
		constr.gridy = 1;
		JLabel header_iadress = new JLabel("Rechnungsadresse");
		this.add(header_iadress, constr);
		
		constr.gridx = 1;
		constr.gridy = 2;
		Adress iadress = this.order.getCustomer().getInvoice_adress();
		JTextArea iadress_area = new JTextArea(String.format(
				"%s\n%s %s\nTel: %s\nMobil: %s\nFax: %s\nE-Mail: %s",
				iadress.getStreet(),
				iadress.getPostal_cd(),
				iadress.getCity(),
				iadress.getTel(),
				iadress.getTel_mobile(),
				iadress.getTel_fax(),
				iadress.getEmail()
				));
		this.add(iadress_area, constr);
		
		constr.gridx = 2;
		constr.gridy = 1;
		JLabel header_dadress = new JLabel("Rechnungsadresse");
		this.add(header_dadress, constr);
		
		constr.gridx = 2;
		constr.gridy = 2;
		Adress dadress = this.order.getCustomer().getInvoice_adress();
		JTextArea dadress_area = new JTextArea(String.format(
				"%s\n%s %s\nTel: %s\nMobil: %s\nFax: %s\nE-Mail: %s",
				dadress.getStreet(),
				dadress.getPostal_cd(),
				dadress.getCity(),
				dadress.getTel(),
				dadress.getTel_mobile(),
				dadress.getTel_fax(),
				dadress.getEmail()
				));
		this.add(dadress_area, constr);
		
		
		// Render LineItems
		String[] pos_header = { "Position #", "Artikel #", "Produktbezeichnung", "Einzelpreis", "Menge", "Gesamtpreis" };
		String[][] pos_data = new String[this.order.getLine_items().size()][6];
		Double gesamt_preis = 0.;
		for(int x = 0; x < this.order.getLine_items().size(); x++) {
			LineItem item = this.order.getLine_items().get(x);
			pos_data[x][0] = Integer.toString(item.getPosition_id());
			pos_data[x][1] = Integer.toString(item.getArticle().getArticleId());
			pos_data[x][2] = item.getArticle().getName();
			pos_data[x][3] = Double.toString(item.getPrice());
			pos_data[x][4] = Integer.toString(item.getAmount());
			pos_data[x][5] = Double.toString(item.getAmount() * item.getPrice());
			gesamt_preis += item.getPrice() * item.getAmount();
		}
		constr.gridx = 0;
		constr.gridy = 3;
		constr.gridwidth = 5;
		JTable table = new JTable(pos_data, pos_header);
		JScrollPane scrollpane = new JScrollPane(table);
		this.add(scrollpane, constr);
		
		// Item Summary
		constr.gridx = 4;
		constr.gridy = 4;
		JLabel ges_label = new JLabel("Gesamt: " + gesamt_preis + "€");
		this.add(ges_label, constr);
		
		constr.gridx = 4;
		constr.gridy = 5;
		JLabel incl_delivery = new JLabel("Mit Lieferkosten: " + (gesamt_preis+this.order.getDelivery_charges()) + " €");
		this.add(incl_delivery, constr);
	}
}
